package com.mai.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mai.realtime.app.BaseAppV1;
import com.mai.realtime.bean.ExamCountBean;
import com.mai.realtime.common.Constant;
import com.mai.realtime.function.DimAsyncFunction;
import com.mai.realtime.util.AtguiguUtil;
import com.mai.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.math.BigDecimal;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * com.mai.realtime.app.dws.Dws_ExamPaperCountWindow
 * 考试相关统计
 */
public class Dws_ExamPaperCountWindow extends BaseAppV1 {
    public static void main(String[] args) {
        new Dws_ExamPaperCountWindow().init(
                4011,
                1,
                "Dws_ExamPaperCountWindow",
                Constant.TOPIC_DWD_COURSE_TEST_EXAM
        );
    }
    @Override
    protected void handle(StreamExecutionEnvironment env, DataStreamSource<String> stream) {
        // 1:封装数据
        SingleOutputStreamOperator<ExamCountBean> examCountStream = ExamToPojo(stream);
        // 2:开窗聚合
        SingleOutputStreamOperator<ExamCountBean> examCountWindow = ExamAggWindow(examCountStream);

        // 3:补充维度（course_name）
        SingleOutputStreamOperator<ExamCountBean> beanStreamWithDim = addDim(examCountWindow);
        // 4:写出
        beanStreamWithDim
                .map(bean -> JSON.toJSONString(bean))
                .print();
                //.addSink(FlinkSinkUtil.getDorisSink("gmall.dws_exam_paper_count_window"));
    }

    private SingleOutputStreamOperator<ExamCountBean> addDim(SingleOutputStreamOperator<ExamCountBean> examCountWindow) {
        //开启异步数据流
        return AsyncDataStream
                .unorderedWait(
                        examCountWindow,
                        new DimAsyncFunction<ExamCountBean>() {
                            @Override
                            public String getTable() {
                                return "dim_course_info";
                            }

                            @Override
                            public String getId(ExamCountBean input) {
                                return input.getCourseId();
                            }

                            @Override
                            public void addDim(JSONObject dim, ExamCountBean input) {
                                input.setCourseName(dim.getString("COURSE_NAME"));
                            }
                        },
                        60
                        , TimeUnit.SECONDS
                );
    }

    private SingleOutputStreamOperator<ExamCountBean> ExamAggWindow(SingleOutputStreamOperator<ExamCountBean> examCountStream) {
        return examCountStream
                .assignTimestampsAndWatermarks(
                        WatermarkStrategy.<ExamCountBean>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                                .withTimestampAssigner((bean, ts) -> bean.getTs() * 1000)
                )
                .keyBy(bean -> bean.getPaperId() + "_" + bean.getCourseId())
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(10)))
                .reduce(new ReduceFunction<ExamCountBean>() {
                            @Override
                            public ExamCountBean reduce(ExamCountBean bean1,
                                                        ExamCountBean bean2) throws Exception {
                                bean1.setUseCount(bean1.getUseCount() + bean2.getUseCount());

                                bean1.setDurationSecCount(bean1.getDurationSecCount().add(bean2.getDurationSecCount()));

                                bean1.setScoreCount(bean1.getScoreCount().add(bean2.getScoreCount()));

                                return bean1;
                            }
                        },
                        new ProcessAllWindowFunction<ExamCountBean, ExamCountBean, TimeWindow>() {
                            @Override
                            public void process(Context ctx,
                                                Iterable<ExamCountBean> elements,
                                                Collector<ExamCountBean> out) throws Exception {

                                ExamCountBean bean = elements.iterator().next();

                                bean.setStt(AtguiguUtil.toDateTime(ctx.window().getStart()));
                                bean.setEdt(AtguiguUtil.toDateTime(ctx.window().getEnd()));
                                bean.setCurDate(AtguiguUtil.toDate(ctx.window().getStart()));

                                out.collect(bean);
                            }
                        }
                );


    }

    private SingleOutputStreamOperator<ExamCountBean> ExamToPojo(DataStreamSource<String> stream) {
       return stream.map(json -> {
            JSONObject obj = JSON.parseObject(json);

            String course_id = obj.getString("course_id");
            String paper_id = obj.getString("paper_id");

            BigDecimal score = new BigDecimal(obj.getString("score"));
            BigDecimal duration_sec = new BigDecimal(obj.getString("duration_sec"));
            Long ts = obj.getLong("ts");

            return new ExamCountBean(
                    course_id,
                    "",
                    "",
                    "", paper_id,
                    "",
                    1L,
                    score,
                    duration_sec,
                    ts);
        });


    }
}

